<?php

namespace Home\Model;
use Think\Model;

class LoginModel extends Model{

    private $_verify;
    public function __construct()
    {
        parent::__construct();
        $this->_verify = new \Think\Verify();
    }

    function Verify($code){

        if(!$code){

            $this->_verify->codeSet = '0123456789';//验证码设置全数字
            $this->_verify->fontSize = 30; // 验证码字体大小
            $this->_verify->length = 3; // 验证码位数
            $this->_verify->useNoise = false; // 关闭验证码杂点

            return $this->_verify->entry();
        }else{

            return $this->_verify->check($code, '');

        }

    }

    //登录
    public function login($data,$verify=''){
        $db = M('user');
        $user = $db->where($data)->find();

        if(!$this->Verify($verify)){
            return 4;
        }

        //存在当前用户
        if($user){
            //密码一致
            if($user['password'] == $data['password']){
                return 1;
            }else{
                return 3;
            }
        }else{
            return 2;
        }
    }
    //注册
    public function rigster($data,$verify){
        $db = M('user');
        $result = $db->where(array('name'=>$data['name']))->find();

        if(!$this->Verify($verify)){
            return 4;
        }

        //用户存在，不能注册
        if($result){
            return 1;
        }else{
            //密码不一致
            if($data['password'] != $data['password2']||$data['password']==''||$data['password2']==''){
                return 3;
            }
            session('username',$data['name']);
            $arr['name'] = $data['name'];
            $arr['password'] = $data['password'];
            $db->add($arr);
            return 2;
        }
    }
} 